<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/6/3
 * Time: 14:34
 */

namespace app\vehicle\logic;
use think\Db;
use app\vehicle\model\User;
use app\vehicle\model\UserIntegral;
use app\vehicle\model\Coupon;
use app\vehicle\model\CouponUser;
use app\vehicle\model\Order;
class couponLogic
{
    public function checkCoupon($data){
        $time = time();
        $coupon = CouponUser::where('end_time','lt',$time)->where('status','eq',0)->select();
        foreach($coupon as $k=>$v){
            CouponUser::where('id','eq',$v['id'])->update(['status'=>2]);
        }
        $check = CouponUser::where('resume','eq',$data['coupon'])->where('status','eq',0)->find();
        if(!$check['id']){
            return '優惠券不存在';
        }

        $info = Order::where('id','eq',$data['order_id'])->find();

        if($info['order_price']>$check['use_min_price']){
            return $check['id'];
        }else{
            return '優惠券不滿足條件，無法使用 ';
        }
    }
    public function getOrderConpons($data,$user){
        $info = Order::get($data['order_id']);
        if($info['order_price']){
           $list = CouponUser::where('status','eq',0)->where('end_time','>=',time())->where('use_min_price','<=',$info['order_price'])->select();

           return $list;
        }
    }
    public function getUseConpons($user){
        $time = time();
        $coupon = CouponUser::where('end_time','lt',$time)->where('status','eq',0)->select();
        foreach($coupon as $k=>$v){
            CouponUser::where('id','eq',$v['id'])->update(['status'=>2]);
        }
        $list = CouponUser::field('*,status as sid')->order('id desc')->where('user_id','eq',$user['id'])->select();
        return $list;
    }
    public function getShopConpons($data){
        $time = time();
        $coupon = CouponUser::where('end_time','lt',$time)->where('status','eq',0)->select();
        foreach($coupon as $k=>$v){
            CouponUser::where('id','eq',$v['id'])->update(['status'=>2]);
        }
        $list = CouponUser::field('*,status as sid')->order('id desc')->where('shop_id','eq',$data['shop_id'])->select();
        return $list;
    }
    public function getIntegralConpons($user){
        $user = User::get($user['id']);
        $coupon = Coupon::where('status','eq',1)->where('integral','elt',$user['integral'])->select();
        return $coupon;
    }

    public function exchange($data,$user){
        $user = User::get($user['id']);
        $coupon = Coupon::where('id','eq',$data['id'])->find();
        if($user['integral']<$coupon['integral']){
            return '積分不足';
        }
        Db::startTrans();
        try {
            User::where('id','eq',$user['id'])->setDec('integral',$coupon['integral']);
            $log['type_id'] = 1;
            $log['user_id'] = $user['id'];
            $log['order_id'] = 0;
            $log['create_time'] = time();
            $log['integral'] = $coupon['integral'];
            UserIntegral::insert($log);

            $insert['user_id'] =$user['id'];
            $insert['title'] = $coupon['title'];
            $insert['coupon_id'] = $coupon['id'];
            $insert['coupon_price'] = $coupon['coupon_price'];
            $insert['use_min_price'] = $coupon['use_min_price'];
            $insert['start_time'] = time();
            $insert['source'] = 1;
            $insert['end_time'] = $insert['start_time']+$coupon['coupon_time']*86400;
            $insert['create_time'] = $insert['start_time'];
            CouponUser::insert($insert);

            Db::commit();
            return 1;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return 0;
        }
    }
}